package com.yl.flink.processor;

import cn.hutool.core.util.NumberUtil;
import com.yl.entity.MultiDataEntity;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;

import java.util.Map;

public class CalTiltFunc implements MapFunction<MultiDataEntity, MultiDataEntity> {

    private final Integer xqc;
    private final Integer yqc;

    public CalTiltFunc(Integer xqc, Integer yqc) {
        this.xqc = xqc;
        this.yqc = yqc;
    }

    /**
     * 计算倾斜角度
     */
    @Override
    public MultiDataEntity map(MultiDataEntity multiDataEntity) throws Exception {
        Map<Integer, Double> rawValues = multiDataEntity.getRawValues();
        Tuple2<Integer, Double> calValues = multiDataEntity.getCalValues();
        // 获取x,y的数据
        Double xValue = rawValues.get(xqc);
        Double yValue = rawValues.get(yqc);
        double tilt = NumberUtil.round(Math.sqrt(Math.pow(xValue, 2) + Math.pow(yValue, 2)), 3).doubleValue();
        multiDataEntity.setCalValues(Tuple2.of(calValues.f0, tilt));
        return multiDataEntity;
    }

}
